﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GesturePad
{
    static class ListViewExtensions
    {
        public enum Direction
        {
            Up,
            Down
        }

        public static void MoveListViewItems(this ListView listView, Direction direction)
        {
            List<ListViewItem> items = new List<ListViewItem>();

            foreach (ListViewItem lvItem in listView.SelectedItems)
            {
                items.Add(lvItem);
            }

            if (direction == Direction.Down)
            {
                for (int i = items.Count - 1; i >= 0; i--)
                {
                    ListViewItem lvItem = items[i];

                    int oldIndex = lvItem.Index;
                    int newIndex;

                    listView.Items.Remove(lvItem);

                    if (oldIndex == listView.Items.Count)
                    {
                        newIndex = 0;
                    }
                    else
                    {
                        newIndex = oldIndex + 1;
                    }

                    listView.Items.Insert(newIndex, lvItem);
                }
            }
            else
            {
                foreach (ListViewItem lvItem in items)
                {
                    int oldIndex = lvItem.Index;
                    int newIndex;

                    listView.Items.Remove(lvItem);

                    if (oldIndex == 0)
                    {
                        newIndex = listView.Items.Count;
                    }
                    else
                    {
                        newIndex = oldIndex - 1;
                    }

                    listView.Items.Insert(newIndex, lvItem);
                }
            }
        }

        public static void RemoveListViewItems(this ListView listView)
        {
            foreach (ListViewItem selectedListViewItem in listView.SelectedItems)
            {
                listView.Items.Remove(selectedListViewItem);
            }
        }
    }
}
